package com.fymxy.question_bank_management.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fymxy.question_bank_management.pojo.ApiResult;
import com.fymxy.question_bank_management.pojo.Course2teacher;
import com.fymxy.question_bank_management.pojo.Student;
import com.fymxy.question_bank_management.pojo.Student2courseKey;
import com.fymxy.question_bank_management.service.*;
import com.fymxy.question_bank_management.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class CourseManageController {

    @Autowired
    private Course2teacherService course2teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private Student2courseService student2courseService;
    @Autowired
    private Paper2courseService paper2courseService;

    @GetMapping("/showCourses/{page}")
    public ApiResult showCourses(@PathVariable("page") Integer page){
        Page<Course2teacher> course2teacherPage = new Page<>(page,10);
        IPage<Course2teacher> course2teacherIPage = course2teacherService.selectAllCourse(course2teacherPage);
        return ApiResultHandler.buildApiResult(200,"查询课程成功",course2teacherIPage);
    }

    @GetMapping("/deleteCourse/{id}")
    public ApiResult deleteCourse(@PathVariable("id") Integer id){
        student2courseService.deleteByCourseKey(id);
        paper2courseService.deleteByCourseKey(id);
        return ApiResultHandler.buildApiResult(200,"请求成功",course2teacherService.deleteByCourseKey(id));
    }

    @GetMapping("/showStudentsOfCourse/{id}/{page}")
    public ApiResult showStudentsOfCourse(@PathVariable("id") Integer id,@PathVariable("page") Integer page){
        Page<Student> studentPage = new Page<>(page,10);
        IPage<Student> studentIPage = studentService.selectStudentByCourse(studentPage,id);
        return ApiResultHandler.buildApiResult(200,"查询课程包含学生成功",studentIPage);
    }

    @PostMapping("/addCourse")
    public ApiResult addCourse(@RequestBody Course2teacher course2teacher){
        return ApiResultHandler.buildApiResult(200,"添加课程成功",course2teacherService.insert(course2teacher));
    }

    @PostMapping("/addStudent2course")
    public ApiResult addStudent2course(@RequestBody Student2courseKey student2courseKey){
        return ApiResultHandler.buildApiResult(200,"学生加入课堂成功",student2courseService.insert(student2courseKey));
    }

    @GetMapping("/deleteStudentFromCourse/{courseNo}/{studentNo}")
    public ApiResult deleteStudentFromCourse(@PathVariable("courseNo") Integer courseNo,@PathVariable("studentNo") Integer studentNo){
        return ApiResultHandler.buildApiResult(200,"删除学生成功",student2courseService.deleteStudentFromCourse(courseNo,studentNo));
    }
}
